﻿/*
 * Copyright (C) 2009-2010 Valerio Fuoglio <valerio.fuoglio@gmail.com>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows.Forms;
using Lastfm.Scrobbling;
using LightScrobbler.Properties;

namespace LightScrobbler
{
    internal class GridViewer : DataGridView, IViewer
    {
        private readonly List<Entry> _entryList;
        private bool _checkable;

        private int _prevWidth;
        private FormWindowState _prevWindowState;

        public GridViewer(List<Entry> entryList, bool checkable)
        {
            _entryList = entryList;
            _checkable = checkable;

            DataSource = _entryList;

            RowHeadersVisible = false;

            DataBindingComplete += Event_DataBindingComplete;
            ReadOnly = true;

            Dock = DockStyle.Fill;
        }

        public FormWindowState ParentWindowState { set; get; }

        #region IViewer Members

        public void Populate()
        {
            BeginInvoke(new ShowSelectedColumnsOnlyDelegate(ShowSelectedColumnsOnly), null);
        }

        public void Close()
        {
        }

        public List<Entry> SelectedEntries
        {
            get { return _entryList; }
        }

        #endregion

        private void ShowSelectedColumnsOnly()
        {
            // Hide all columns 
            foreach (DataGridViewColumn column in Columns)
            {
                column.Visible = false;
            }

            // Turn to visible only needed columns
            StringCollection columnsShown = Settings.Default.ColumnsShown;

            foreach (string col in columnsShown)
            {
                Columns[col].Visible = true;
            }

            if (columnsShown.Contains("TimeStarted"))
                Columns["TimeStarted"].HeaderText = "Played";
        }

        private void ResizeGrid()
        {
            if (_prevWidth == 0)
                _prevWidth = Width;
            if (_prevWidth == Width)
                return;

            int fixedWidth = 0;
            bool scrollBarVisible = false;

            foreach (Control c in Controls)
            {
                var a = new VScrollBar();
                if (c.GetType() == a.GetType())
                {
                    scrollBarVisible = c.Visible;
                    break;
                }
            }

            if (scrollBarVisible)
            {
                fixedWidth = SystemInformation.VerticalScrollBarWidth + RowHeadersWidth + 4 -
                             RowHeadersWidth;
            }
            else
            {
                fixedWidth = RowHeadersWidth + 3 - RowHeadersWidth;
            }

            int mul = 100*(Width - fixedWidth)/(_prevWidth - fixedWidth);
            int columnWidth;
            int total = 0;
            DataGridViewColumn lastVisibleCol = null;

            for (int i = 0; i < ColumnCount; i++)
                if (Columns[i].Visible)
                {
                    columnWidth = (Columns[i].Width*mul + 50)/100;
                    Columns[i].Width = Math.Max(columnWidth, Columns[i].MinimumWidth);
                    total += Columns[i].Width;
                    lastVisibleCol = Columns[i];
                }
            if (lastVisibleCol == null) return;
            columnWidth = Width - total + lastVisibleCol.Width - fixedWidth;
            lastVisibleCol.Width = Math.Max(columnWidth, lastVisibleCol.MinimumWidth);
            _prevWidth = Width;
        }

        public void Event_ResizeEnd(object sender, EventArgs e)
        {
            ResizeGrid();
        }

        public void Event_Resize(object sender, EventArgs e, FormWindowState WindowState)
        {
            if (ParentWindowState != _prevWindowState && ParentWindowState != FormWindowState.Minimized)
            {
                _prevWindowState = ParentWindowState;
                ResizeGrid();
            }
        }

        private void Event_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            ResizeGrid();
        }

        #region Nested type: ShowSelectedColumnsOnlyDelegate

        private delegate void ShowSelectedColumnsOnlyDelegate();

        #endregion
    }
}